#include <iostream>
using namespace std;
#include "UdpServer.hpp"
#include<memory>
#include <vector>
void Usage(string proc)
{
    cout<<"\n\rusage:"<<proc<<" serverport"<<endl;
}
string Handler(string& str)
{
    string res=" server get a message:";
    res+=str;
    return res;
}
bool SafeCheck(const string&cmd)
{
    vector<string> checklist={
        "rm","kill","mv","top","ulink"
    } ;
    for(auto &e:checklist)
    {
        if(e==cmd)return false;
    }
    return true;
}
string Excutecommand(const string& cmd)
{
    //获取输入的命令并执行，返回执行结果
    bool safe=SafeCheck(cmd);//检查命令可靠性
    if(!safe)return "error";
    //命令安全可执行
    FILE*fp=popen(cmd.c_str(),"r");//创建子进程让子进程执行命令并把结果写入写入文件，把文件返回给父进程
    if(fp==nullptr)return "error";
    string result;
    char buffer[1024];
    while(true)
    {
    char* ok=fgets(buffer,sizeof(buffer),fp);//按行读？读完一次后，第二次从哪开始读？
    if(nullptr==ok)break;//读取结束
    result+=buffer;
    }
    pclose(fp);
    return result;

}
//udpserver serverport
int main(int argc ,char*argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t serverport=stoi(argv[1]);
    unique_ptr<UdpServer> svr(new UdpServer(serverport));
    svr->Init();
    svr->Run();
    return 0;
}